package com.google.shoopinggoods.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class KLog {

	public static final String TAG = "quickey";
	public static final String PATH = Environment.getExternalStorageDirectory()+ File.separator+"Android"+ File.separator+"QUiCKEY"+ File.separator;
	public static final String LOG_FILE = PATH+"QuickeyLog.txt";
	public static final String LOG_CONFIG = PATH + "QuickeyLog.property";

	public enum Tag {
		QUICKEY("QUICKEY", true), KLOGIN("KLOGIN", false), KLOGOUT(
				"KLOGOUT", false), KPAY("KPAY", false), KSECURITY("KSECURITY", false), KUSERCENTER(
				"KUSERCENTER", false), KHTTPCLIENT("KHTTPCLIENT", false), KACTIVITY("KACTIVITY", false), KPLATFORM("KPLATFORM", false);

		public final String name;
		public boolean flag;

		private Tag(String n, boolean b) {
			name = n;
			flag = b;
		}

		public String getName() {
			return name;
		}
	}

	private static File config = null;
	private static File logFile = null;

	private static SimpleDateFormat format;


	public static boolean showAllLogs = true;

	static{
		Log.e("QUICKEY","LOG_CONFIG"+LOG_CONFIG.toString());
		config = new File(LOG_CONFIG);

		if (config.exists()) {

			showAllLogs = true;

//			Log.i(TAG, "LogConfig exists   + "+LOG_FILE.toString());

			logFile = new File(LOG_FILE );

//			Log.i(TAG, "LogConfig exists logFile:::"+logFile.toString());
//			format = new SimpleDateFormat("hh:mm:ss--SSS");
			format = new SimpleDateFormat("现在是yyyy年MM月dd日 HH(hh)时   mm分 ss秒 S毫秒   星期E 今年的第D天  这个月的第F星期   今年的第w个星期");

			try {
				Properties property = new Properties();
				property.load(new FileInputStream(config));

				Tag.QUICKEY.flag = getProperty(property, Tag.QUICKEY.getName());
				Tag.KLOGIN.flag = getProperty(property, Tag.KLOGIN.getName());
				Tag.KLOGOUT.flag = getProperty(property, Tag.KLOGOUT.getName());
				Tag.KPAY.flag = getProperty(property, Tag.KPAY.getName());
				Tag.KSECURITY.flag = getProperty(property, Tag.KSECURITY.getName());
				Tag.KUSERCENTER.flag = getProperty(property, Tag.KUSERCENTER.getName());
				Tag.KHTTPCLIENT.flag = getProperty(property, Tag.KHTTPCLIENT.getName());
				Tag.KACTIVITY.flag = getProperty(property, Tag.KACTIVITY.getName());
				Tag.KPLATFORM.flag = getProperty(property, Tag.KPLATFORM.getName());

			} catch (IOException e) {
				e.printStackTrace();
			}

//			writeFile("\r\n");
//			writeFile("-------------------------------------------------"
//					+ format.format(new Date(System.currentTimeMillis()))
//					+ "----------------------------------------------------\r\n");
//			writeFile("\r\n");
//			if(showAllLogs)
//				showAllLogs = false;

		} else {
			Log.i(TAG, "LogConfig not exists");
		}
	}

	private static Boolean getProperty(Properties property, String name) {
		String pro = property.getProperty(name, "false");
		return Boolean.parseBoolean(pro.toLowerCase());
	}

	public static void writeFile(String msg) {

		Log.i(TAG, "writeFile(String msg)");
		synchronized (logFile) {
			FileWriter writer = null;
			try {
				if(logFile == null)
					return;
				writer = new FileWriter(logFile, true);
				Log.i(TAG, "writer.write______)"+msg.toString());
				writer.write(msg);
				writer.write(format.format(new Date(System.currentTimeMillis())) + " : " + msg + "\r\n\r\n");
				writer.flush();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (writer != null)
						writer.close();
					writer = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void e(Tag tag, String msg) {
		Log.e("MrGao","tag.flag::"+tag.flag+",,showAllLogs:::"+showAllLogs);
		if (tag.flag || showAllLogs) {
			Log.e(TAG + "-" + tag.name, msg);

//			writeFile(tag.name+"		"+msg);
		}
	}

	public static void w(Throwable e) {

		if (showAllLogs) {
			Log.w(TAG, e.toString());
//			writeFile("**  **  **  **  **  **  **  **  **  **  **  **  **  \r\n");
//			writeFile(TAG+"		"+e.toString());
//			StackTraceElement[] stackTrace = e.getStackTrace();
//			for (StackTraceElement stackTraceElement : stackTrace) {
//				Log.w(TAG, stackTraceElement.toString());
//				writeFile(TAG+"		"+stackTraceElement.toString());
//			}
//			writeFile("**  **  **  **  **  **  **  **  **  **  **  **  **  \r\n");
		}
	}

	public static void d(Tag tag, String msg) {

		if (tag.flag || showAllLogs) {
			Log.d(TAG + "-" + tag.name, msg);
//			writeFile(tag.name+"		"+msg);
		}
	}

	public void readAndWriteLogs() {
		Process mLogcatProc = null;
		BufferedReader reader = null;
		try {
			mLogcatProc = Runtime.getRuntime().exec(
					new String[] { "logcat", "-d",
							"AndroidRuntime:E [Your Log Tag Here]:V *:S" });
			reader = new BufferedReader(new InputStreamReader(
					mLogcatProc.getInputStream()));
			String line;
			final StringBuilder log = new StringBuilder();
			String separator = System.getProperty("line.separator");
			while ((line = reader.readLine()) != null) {
				log.append(line);
				log.append(separator);
			}
			// do whatever you want with the log. I'd recommend using Intents to
			// create an email
		} catch (IOException e) {
		} finally {
			if (reader != null)
				try {
					reader.close();
				} catch (IOException e) {
				}
		}
	}

	public static void showToast(Context context, String msg) {
		if (showAllLogs)
			Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}

}
